package com.snj.visangedu.news;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.snj.visangedu.R;


public class NewsAdapter extends BaseAdapter {
	
	String tag = "NewsAdapter";
	
	Context context;
	int layout;
	
	
	ArrayList<NewsItem> mNewsList;
	
	LayoutInflater mInflater = null;
	
	public NewsAdapter(Context _context, int _layout, ArrayList<NewsItem> _newslist) {
		// TODO Auto-generated constructor stub
		
		this.context = _context;
		this.layout = _layout;
		this.mNewsList = _newslist;
		
	}
	
	public void Insert(ArrayList<NewsItem> mList)
	{
		mNewsList.addAll(mList);
	}
	
	public void SetList(ArrayList<NewsItem> mList)
	{
		mNewsList = mList;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mNewsList.size();
	}

	public NewsItem getItem(int position) {
		// TODO Auto-generated method stub
		return mNewsList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		NewsHolder holder = null;
		
		if(convertView == null)
		{
			mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(layout, parent, false);
			
			holder = new NewsHolder();
			
			holder.img_news = (ImageView)convertView.findViewById(R.id.img_news);
			holder.txt_title = (TextView)convertView.findViewById(R.id.txt_newstitle);
			holder.txt_content = (TextView)convertView.findViewById(R.id.txt_newssubtitle);
			
			convertView.setTag(holder);
			
			
		}
		else
		{
			holder = (NewsHolder)convertView.getTag();
		}
		
		if(!mNewsList.get(position).GetDrawn())
		{
		
			Animation ani = AnimationUtils.loadAnimation(context, R.anim.listani);
			ani.setStartOffset(position * 50);
			convertView.startAnimation(ani);
			mNewsList.get(position).SetDrawn(true);
		}
		
		Log.d(tag, "Created news item");
		
		//Log.d("News", "Show Image");
		holder.img_news.setImageBitmap(mNewsList.get(position).GetImage());
		holder.txt_title.setText(mNewsList.get(position).GetTitle());
		holder.txt_content.setText(mNewsList.get(position).GetContent());
		
		
		
		
		return convertView;
	}
	
	
}
